package middlerware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"pkg"
	"types"
)

func MiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.PostForm("token")
		if token == "" {
			c.JSON(http.StatusBadRequest, types.Router{
				Code: -1,
				Msg:  "token为空",
				Data: nil,
			})
			return
		}
		c.Next()
		payload, err := pkg.PareJwtToken("2204a", token)
		if err != nil {
			c.JSON(http.StatusBadRequest, types.Router{
				Code: -1,
				Msg:  "token失效",
				Data: nil,
			})
			return
		}
		c.Set("token", payload)
		c.Next()
	}
}
